package app.controller.sys;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import app.model.sys.SysUser;
import app.service.sys.SysUserService;
import base.JsonResult;
import base.PageInfo;

@Controller
@RequestMapping("/sys/user")
public class SysUserController {
	
	private Logger logger = Logger.getLogger(SysUserController.class);
	
	@Resource
	private SysUserService sysUserService;
	
	@ResponseBody
	@RequestMapping("/queryLoginUserList.do")
	public JsonResult queryLoginUserList(){
		return new JsonResult(sysUserService.queryLoginUserList());
	}
	
	@ResponseBody
	@RequestMapping(value = "/login.do" ,method = RequestMethod.POST)
	public JsonResult login(String account,String password,HttpSession session){
		try {
			//超级管理员，密码123456
			if("admin".equals(account)&& "e10adc3949ba59abbe56e057f20f883e".equals(password)){
				SysUser sysUser = new SysUser();
				sysUser.setAccount(account);
				sysUser.setUserName("admin");
				session.setAttribute("sysUser", sysUser);
			}else{
				SysUser sysUser = sysUserService.login(account,password);
				session.setAttribute("sysUser", sysUser);
			}
			return new JsonResult("操作成功");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new JsonResult(e.getMessage(),false);
		}
	}
	
	@ResponseBody
	@RequestMapping("/view.do")
	public JsonResult view(String id){
		SysUser sysUser = sysUserService.queryEntityById(id);
		return new JsonResult("操作成功",sysUser);
	}
	
	@ResponseBody
	@RequestMapping(value = "/save.do" ,method = RequestMethod.POST)
	public JsonResult register(SysUser sysUser){
		if(StringUtils.isEmpty(sysUser.getId())){
			sysUserService.saveEntity(sysUser);
		}else{
			sysUserService.updateEntity(sysUser);
		}
		return new JsonResult("操作成功");
	}
	
	@ResponseBody
	@RequestMapping("/queryByPage.do")
	public PageInfo queryLoginUserPage(PageInfo pageInfo){
		return sysUserService.queryLoginUserPage(pageInfo);
	}
	
	@ResponseBody
	@RequestMapping("/del.do")
	public JsonResult del(String ids){
		try {
			sysUserService.del(ids);
			return new JsonResult("操作成功");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new JsonResult(e.getMessage(),false);
		}
	}
}
